package com.snap.awesomeserial.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.snap.awesomeserial.R;
import com.snap.awesomeserial.bean.TemplateEntity;
import com.snap.awesomeserial.ui.DemoActivity;
import com.snap.awesomeserial.ui.DemoEditActivity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class DemoAdapter extends RecyclerView.Adapter<DemoAdapter.DemoHolder> {

    private List<TemplateEntity> demoList;
    private Context context;

    public DemoAdapter(Context context, List<TemplateEntity> list) {
        this.context = context;
        this.demoList=list;
    }

    @NonNull
    @Override
    public DemoHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.demo_item, parent, false);
        return new DemoHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull DemoHolder holder, int position) {
        if (demoList==null||demoList.size() == 0 || position == demoList.size()) {
            holder.deleteDemoTv.setVisibility(View.GONE);
            holder.itemView.setOnClickListener(v -> {
                Intent intent = new Intent();
                intent.putExtra("TemplateEntity",(demoList==null||demoList.size()==position)?null:demoList.get(position).getName());
                intent.setClass(context, DemoEditActivity.class);
                context.startActivity(intent);
            });
            holder.demoItem.setBackground(context.getDrawable(R.drawable.ic_demo_add));
            holder.demoName.setVisibility(View.GONE);
            holder.demoNumber.setVisibility(View.GONE);
            return;
        }

        TemplateEntity demo = demoList.get(position);
        holder.demoNumber.setText("#"+String.format("%02d",position+1));
        holder.demoName.setVisibility(View.VISIBLE);
        holder.demoName.setText(demo.getName());
        holder.demoNumber.setVisibility(View.VISIBLE);
        holder.demoItem.setBackground(context.getDrawable(R.drawable.bg_demo));
//        holder.deleteDemoTv.setVisibility(View.VISIBLE);
        holder.itemView.setOnClickListener(v -> {
            Intent intent = new Intent();
            intent.putExtra("fileName", demo.getName());
            intent.setClass(context, DemoActivity.class);
            context.startActivity(intent);
        });
    }



    @Override
    public int getItemCount() {
        return (demoList == null|| demoList.size()==0)? 1 : (demoList.size() + 1);
    }

    public void bindDataList(List<TemplateEntity> demoList) {
        this.demoList = demoList;
        notifyDataSetChanged();
    }

    static class DemoHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.demo_name_tv)
        TextView demoName;
        @BindView(R.id.demo_number_tv)
        TextView demoNumber;
        @BindView(R.id.delete_demo_tv)
        TextView deleteDemoTv;
        @BindView(R.id.demo_item)
        FrameLayout demoItem;

        DemoHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
